@echo off
::================需更改参数(不要加引号和空格)================

::参数设置
set user=db_user
set password=123456
set port=1521
set datafile_path=d:\opt\database_bak
::是否删除.sql文件(包含子目录): ON开启,其他字符不开启
set rf_switch=ON
::数据保留天数(按文件修改时间计算)
set expire_day=7


::================非开发人员勿改================
set bak_path=%datafile_path%\%user%
set error_log=%bak_path%\%user%_error.log
set info_log=%bak_path%\%user%_info.log

::判断文件夹不存在则创建
if not exist %bak_path% (
	mkdir %bak_path%
	echo %date% %time% 创建文件夹%bak_path% >> %info_log%
)


::判断oralce是否开启
netstat -aon | findstr %port%

if %errorlevel% neq 0 (
	echo %date% %time% 没有监听到Oracle端口,请检查Oralce服务是否启动 >>%error_log%
)

::执行备份
set time_name=%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%

exp %user%/%password% file=%bak_path%\%user%_%time_name%.dmp log=%bak_path%\%user%_%time_name%.dmp.log

if %errorlevel% neq 0 (
	echo %date% %time%   数据库%bak_path%\%user%_%time_name%.dmp备份失败>>%error_log%
)

::删除过期文件
if %rf_switch%==ON goto RF_METHOD

:RF_METHOD
forfiles /s /d -%expire_day% /m *.dmp* /p "%bak_path%" /c "cmd /c del @path"
goto :EOF